
/**
 * Copyright 2014-2020  the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.upb.webserver.account.developer;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.account.PasswordInfo;
import com.upb.webserver.common.pojo.account.developer.*;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Log4j2
@RestController
@RequestMapping(value = "developer")
public class DeveloperController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private DeveloperService developerService;

    /**
     * register developer.
     *
     * @param param
     * @param result
     * @return
     */
    @PostMapping(value = "/register")
    public BaseResponse register(@RequestBody @Valid ReqDeveloperRegisterVO param, BindingResult result) {
        log.info("start exec method [register]. param:{}", JsonUtils.objToString(param));
        RspDeveloperVO rspDeveloperVO = developerService.register(param);
        BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS);
        baseResponse.setData(rspDeveloperVO);
        log.info("success exec method [register]. result:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }


    /**
     * @param param
     * @param request
     * @param result
     * @return
     */
    @PostMapping(value = "/add")
    public BaseResponse addByOtherAccount(@RequestBody @Valid ReqAddDeveloperVO param, HttpServletRequest request, BindingResult result) {
        log.info("start exec method [addByOtherAccount]. param:{}", JsonUtils.objToString(param));
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        RspDeveloperVO rspDeveloperVO = developerService.addByOtherAccount(accountCommon.getPlatform(), accountCommon.getId(), param);
        BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS);
        baseResponse.setData(rspDeveloperVO);
        log.info("success exec method [addByOtherAccount]. result:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }


    /**
     * @param request
     * @return
     */
    @GetMapping(value = "/self/detail")
    public BaseResponse queryDeveloperDetail(HttpServletRequest request) {
        log.info("start exec method [queryDeveloperDetail].");
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        RspDeveloperVO rspDeveloperVO = developerService.queryDeveloperDetail(accountCommon.getId());

        BaseResponse baseResponse = BaseResponse.initSuccess();
        baseResponse.setData(rspDeveloperVO);
        log.info("success exec method [queryDeveloperDetail]. response:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }

    /**
     * @param param
     * @param request
     * @param result
     * @return
     */
    @PatchMapping
    public BaseResponse modifyDeveloper(@RequestBody @Valid ReqModifyDeveloperVO param, HttpServletRequest request, BindingResult result) {
        log.info("start exec method [register]. param:{}", JsonUtils.objToString(param));
        //get AccountCommon
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        developerService.modifyDeveloper(param, accountCommon.getId());
        log.info("success exec method [register].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }

//
//    /**
//     * add
//     *
//     * @param param
//     * @param result
//     * @return
//     */
//    @PostMapping(value = "/contactPerson")
//    public BaseResponse addContactPerson(@RequestBody @Valid ReqAddContactPersonVO param, HttpServletRequest request, BindingResult result) {
//        log.info("start exec method [addContactPerson]. param:{}", JsonUtils.objToString(param));
//        //get current user
//        AccountCommon currentAccount = accountService.getAccountCommon(request);
//        developerService.addContactPerson(param, currentAccount.getId());
//        log.info("success exec method [addContactPerson].");
//        return new BaseResponse(ConstantCode.SUCCESS);
//    }

    /**
     * @param companyDeveloperId
     * @return
     */
    @DeleteMapping(value = "contactPerson/{companyDeveloperId}")
    public BaseResponse deleteContactPerson(@PathVariable("companyDeveloperId") Integer companyDeveloperId) {
        log.info("start exec method [deleteContactPerson]. companyDeveloperId:{}", companyDeveloperId);
        developerService.deleteContactPerson(companyDeveloperId);
        log.info("success exec method [deleteContactPerson].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }


    /**
     * @param pageNumber
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping(value = "list")
    public BasePageResponse queryDeveloperByPage(@RequestParam("pageNumber") Integer pageNumber,
                                                 @RequestParam("pageSize") Integer pageSize,
                                                 @RequestParam(value = "status", required = false) Byte status,
                                                 HttpServletRequest request) {
        log.info("start exec method [queryDeveloperByPage]. pageNumber:{} pageSize:{} status:{}", pageNumber, pageSize, status);
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        BasePageResponse response = developerService.queryByPage(accountCommon.getPlatform(), accountCommon.getRoleName(), accountCommon.getId(), status, pageNumber, pageSize);
        log.info("success exec method [queryDeveloperByPage]. result:{}", JsonUtils.objToString(response));
        return response;
    }


//    /**
//     * query contact list.
//     *
//     * @param pageNumber
//     * @param pageSize
//     * @param status
//     * @param request
//     * @return
//     */
//    @GetMapping(value = "contactPerson/list")
//    public BasePageResponse queryContactPersonByPage(@RequestParam("pageNumber") Integer pageNumber,
//                                                     @RequestParam("pageSize") Integer pageSize,
//                                                     @RequestParam(value = "status", required = false) Byte status,
//                                                     HttpServletRequest request) {
//        log.info("start exec method [queryContactPersonByPage]. pageNumber:{} pageSize:{} status:{}", pageNumber, pageSize, status);
//
//        //get AccountCommon
//        AccountCommon accountCommon = accountService.getAccountCommon(request);
//        //query contactPerson
//        BasePageResponse response = developerService.queryContactPersonByPage(accountCommon.getId(), status, pageNumber, pageSize);
//        log.info("success exec method [queryContactPersonByPage]. result:{}", JsonUtils.objToString(response));
//        return response;
//    }


    @PatchMapping(value = "freeze")
    public BaseResponse freeze(@RequestBody @Valid ReqFreezeVO param, BindingResult result) {
        log.info("start exec method [freeze]. param:{}", JsonUtils.objToString(param));
        developerService.freeze(param.getDeveloperId(), param.getDescription());
        log.info("success exec method [freeze]");
        return BaseResponse.initSuccess();
    }


    @PatchMapping(value = "unFreeze")
    public BaseResponse unfreeze(@RequestBody @Valid ReqFreezeVO param, BindingResult result) {
        log.info("start exec method [unfreeze]. param:{}", JsonUtils.objToString(param));
        developerService.unfreeze(param.getDeveloperId(), param.getDescription());
        log.info("success exec method [unfreeze]");
        return BaseResponse.initSuccess();
    }


    /**
     * @param param
     * @param request
     * @param result
     * @return
     */
    @PatchMapping("changePassword")
    public BaseResponse updatePassword(@RequestBody @Valid PasswordInfo param, HttpServletRequest request, BindingResult result) {
        log.info("start exec method [updatePassword]. param:{}", JsonUtils.objToString(param));
        //get AccountCommon
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        developerService.updatePassword(accountCommon.getId(), param.getOldAccountPwd(), param.getNewAccountPwd());
        log.info("success exec method [register].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }
}
